package com.pro.schedule.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pro.schedule.entity.Event;
import com.pro.schedule.filter.ResultUtil;
import com.pro.schedule.mapper.EventMapper;
import com.pro.schedule.service.EventService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class EventServiceImpl implements EventService {

    @Resource
    ResultUtil resultUtil;
    @Resource
    EventMapper eventMapper;

    @Override
    public List<Event> getEventList(String token, LocalDate eventDate) {
        Long userId = resultUtil.getUserId(token);
        List<Event> eventList = eventMapper.getEventList(userId, eventDate);
        for (Event item : eventList) {
            item.setUserId(0L);
        }
        return eventList;
    }

    @Override
    public Event getEventInfo(String token) {
        Long userId = resultUtil.getUserId(token);
        LocalDateTime nowTime = LocalDateTime.now();
        Event event = eventMapper.getEventInfo(userId, nowTime);
        if(event!=null){
            event.setUserId(0L);
        }
        return event;
    }

    @Override
    public String saveOrUpdateEvent(String token, Event event) throws Exception {
        Long userId = resultUtil.getUserId(token);
        event.setUserId(userId);
        List<Event> eventList = eventMapper.getEventList(userId, event.getEventDate());
        LocalDateTime start1 = event.getEventStart();
        LocalDateTime end1 = event.getEventEnd();
        for (Event item : eventList) {
            if (Objects.equals(item.getEventId(), event.getEventId())) {
                continue;
            }
            LocalDateTime start2 = item.getEventStart();
            LocalDateTime end2 = item.getEventEnd();
            if ((start1.isBefore(end2) && end1.isAfter(start2)) || (start2.isBefore(end1) && end2.isAfter(start1))) {
                throw new Exception("和其他事件重叠");
            }
        }
        if (event.getEventId() == null) {
            int result = eventMapper.insert(event);
            return resultUtil.getResult(result, "保存");
        } else {
            int result = eventMapper.updateById(event);
            return resultUtil.getResult(result, "修改");
        }
    }

    @Override
    public String deleteEvent(Integer eventId) throws Exception {
        int result = eventMapper.deleteById(eventId);
        return resultUtil.getResult(result, "删除");
    }

    @Override
    public String clearEvent(String token, LocalDate eventDate) throws Exception {
        Long userId = resultUtil.getUserId(token);
        int result = eventMapper.clearEvent(userId, eventDate);
        return resultUtil.getResult(result, "清除");
    }

    @Override
    public int getEventCount(String token) {
        Long userId = resultUtil.getUserId(token);
        QueryWrapper<Event> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId).eq("event_date", LocalDate.now());
        return eventMapper.selectCount(wrapper);
    }
}
